package com.taurus.core.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;


/**
 * 文件处理工具
 * 
 * 
 *
 */
public class FileUtil {

	/**
	 * delete file
	 * @param file
	 */
	public static void delete(File file) {
		if (file != null && file.exists()) {
			if (file.isFile()) {
				file.delete();
			} else if (file.isDirectory()) {
				File files[] = file.listFiles();
				if (files != null) {
					for (int i = 0; i < files.length; i++) {
						delete(files[i]);
					}
				}
				file.delete();
			}
		}
	}

	/**
	 * get file extension 
	 * @param fileFullName
	 * @return
	 */
	public static String getFileExtension(String fileFullName) {
		if (StringUtil.isEmpty(fileFullName)) {
			throw new RuntimeException("fileFullName is empty");
		}
		return getFileExtension(new File(fileFullName));
	}

	/**
	 * get file extension 
	 * @param file
	 * @return
	 */
	public static String getFileExtension(File file) {
		if (null == file) {
			throw new NullPointerException();
		}
		String fileName = file.getName();
		int dotIdx = fileName.lastIndexOf('.');
		return (dotIdx == -1) ? StringUtil.Empty : fileName.substring(dotIdx + 1);
	}

	/**
	 * read utf text file
	 * @param path
	 * @return
	 * @throws Exception
	 */
	public static String readTxt(String path) throws Exception {
		InputStream is = new FileInputStream(path);
		try {
			byte[] bytes = new byte[is.available()];
			is.read(bytes);
			String str = StringUtil.getString(bytes);;
			return str;
		}finally {
			is.close();
		}
	}
	
	/**
	 * read binary file
	 * @param path
	 * @return
	 * @throws Exception
	 */
	public static byte[] readBytes(String path) throws Exception {
		InputStream is = new FileInputStream(path);
		try {
			byte[] bytes = new byte[is.available()];
			is.read(bytes);
			return bytes;
		}finally {
			is.close();
		}
	}
	
	/**
	 * write binary file
	 * @param path
	 * @param bytes
	 * @throws Exception
	 */
	public static void writeBytes(String path,byte[] bytes) throws Exception {
		OutputStream os = new FileOutputStream(path);
		try {
			os.write(bytes);
		}finally {
			os.close();
		}
	}
	
	/**
	 * write utf text file
	 * @param path
	 * @param text
	 * @throws Exception
	 */
	public static void writeTxt(String path,String text) throws Exception {
		OutputStream os = new FileOutputStream(path);
		try {
			os.write(StringUtil.getBytes(text));
		}finally {
			os.close();
		}
	}
	
	/**
	 * 遍历文件
	 * @param path
	 * @param suffixList 指定的扩展名
	 * @return
	 */
	public static FindFileVisitor findFiles(String path,List<String> suffixList) throws IOException{
		FindFileVisitor findJavaVisitor = new FindFileVisitor(suffixList);  
		Path startingDir = Paths.get(path); 
		Files.walkFileTree(startingDir, findJavaVisitor);
        return findJavaVisitor;
	}
	
	public static class FindFileVisitor extends SimpleFileVisitor<Path> {  
	    private List<String> filenameList = new ArrayList<String>();  
	    private List<String> fileSuffix = null;  
	  
	    public FindFileVisitor(List<String> fileSuffix) {  
	        this.fileSuffix = fileSuffix;  
	    }  
	  
	    @Override  
	    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {  
	    	for(String suffix : fileSuffix) {
	    		 if (file.toString().endsWith(suffix)) {  
	 	            filenameList.add(file.toString());  
	 	        }  
	    	}
	        return FileVisitResult.CONTINUE;  
	    }  
	  
	    public List<String> getFilenameList() {  
	        return filenameList;  
	    }  
	  
	    public void setFilenameList(List<String> filenameList) {  
	        this.filenameList = filenameList;  
	    }  
	}  
}
